<template>
	<view class="normal-login-container">
		<view class="logo-content align-center justify-center flex">
<!--			<text class="title">登录</text>-->
		</view>
    <view class="flexcenter">
      <image  style="height: 100px;width:100px;border-radius: 8px" src="@/static/images/logo.png"/>
    </view>
    <view class="flexcenter">
     <text class="margin-top-lg text-blue font-bold text-lg">智慧物联-企业版</text>
      <text class="margin-top-xs text-black text-sm">智慧安全，共筑未来</text>
    </view>

		<view class="login-form-content">
      <uni-forms ref="form" :value="loginForm" labelWidth="80px" border>

        <uni-forms-item name="mobile" label="手机号" placeholder="输入手机号" class="bg-white"  style="border-radius: 6px" label-align="center">
          <uni-easyinput type="text" v-model="loginForm.mobile" placeholder="请输入手机号" class="padding-right" />
        </uni-forms-item>

         <uni-forms-item name="password" label="密码"  placeholder="输入密码"  class="bg-white margin-top"  style="border-radius: 6px" label-align="center">
           <uni-easyinput type="password" v-model="loginForm.password" placeholder="请输入密码" class="padding-right" />
         </uni-forms-item>
      </uni-forms>
			<Verify @success="pwdLogin" :mode="'pop'" :captchaType="'blockPuzzle'"
				:imgSize="{ width: '330px', height: '155px' }" ref="verify"></Verify>
			<view class="action-btn">
				<button @click="handleLogin"
                :loading="btnLoading"
                :disabled="btnDisable"  class="login-btn cu-btn block bg-blue lg round">登录</button>
			</view>
		</view>
		<!-- <view class="xieyi text-center">
			<text class="text-grey1">登录即代表同意</text>
			<text @click="handleUserAgrement" class="text-blue">《用户协议》</text>
			<text @click="handlePrivacy" class="text-blue">《隐私协议》</text>
		</view> -->

    <view class="margin-xl text-right">
      <text class="text-grey1 padding" @click="forgetPwd">忘记密码</text>
    </view>
	</view>
</template>

<script>
	import Verify from "@/components/verifition/Verify"
  import MQTT from 'utils/mqtt'
	export default {
		name: 'Login',
		components: {
			Verify
		},

		data() {
			return {
				btnLoading:false,
				captchaEnabled: false, // 验证码开关 TODO 芋艿：需要抽到配置里
				globalConfig: getApp().globalData.config,
				loginForm: {
					mobile: "",
					password: "",
					captchaVerification: ""
				}
			}
		},
    computed:{
      btnDisable(){
        return !(this.loginForm.mobile && this.loginForm.password)
      }
    },
		methods: {
      // 隐私协议
      handlePrivacy() {
        let site = this.globalConfig.appInfo.agreements[0]
        this.$tab.navigateTo(`/pages/common/webview/index?title=${site.title}&url=${site.url}`)
      },
      // 用户协议
      handleUserAgrement() {
        let site = this.globalConfig.appInfo.agreements[1]
        this.$tab.navigateTo(`/pages/common/webview/index?title=${site.title}&url=${site.url}`)
      },
      // 登录方法
      async handleLogin() {
        if (this.loginForm.mobile === "") {
          this.$modal.msgError("请输入您的手机号")
        } else if (this.loginForm.password === "") {
          this.$modal.msgError("请输入您的密码")
        } else {
          // 显示验证码
          if (this.captchaEnabled) {
            this.$refs.verify.show()
          } else { // 直接登录
            await this.pwdLogin({})
          }
        }
      },
      // 密码登录
      async pwdLogin(captchaParams) {
        // this.btnLoading = true
        this.$modal.loading("登录中，请耐心等待...")
        // 执行登录
        this.loginForm.captchaVerification = captchaParams.captchaVerification
        this.$store.dispatch('Login', this.loginForm).then(() => {
          // this.btnLoading = false
          this.$modal.closeLoading()
          this.loginSuccess()
        })
      },
      // 登录成功后，处理函数
      loginSuccess(result) {
        // 设置用户信息
        this.$store.dispatch('GetInfo').then(res => {
          // 获取所有字典
          this.$store.dispatch('GetDictMap') //跳转时候已经请求了
          console.log('GetInfores>>>>', res)

          this.$tab.reLaunch('/pages/home/index')
        })
      },

      forgetPwd(){
        uni.navigateTo({
          url: '/pages/mine/forget-pwd/forget-pwd'
        })
      }
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #ffffff;
	}

	.normal-login-container {
    width: 100%;
    height: 100vh;
    background-image: url('/static/images/mine/login.png');
    background-size: cover;

		.logo-content {
			width: 100%;
			font-size: 21px;
			text-align: center;
			padding-top: 15%;

			image {
				border-radius: 4px;
			}

			.title {
				margin-left: 10px;
			}
		}

		.login-form-content {
			text-align: center;
			margin: 20px auto;
			margin-top: 15%;
			width: 100%;
      padding-left: 40rpx;
      padding-right: 40rpx;

			}

			.login-btn {
				margin-top: 40px;
				height: 45px;
			}

			.xieyi {
				color: #333;
				margin-top: 20px;
			}
		}

		.easyinput {
			width: 100%;
		}


	.login-code-img {
		height: 45px;
	}
</style>